package com.ogawa.joypal.distributionnetwork.activity

import android.Manifest
import android.content.Context
import android.net.wifi.WifiManager
import android.view.MotionEvent
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.easepal.softaplib.wifi.WiFiManager
import com.ogawa.base.base.BaseActivity
import com.ogawa.base.utils.MiuiUtils
import com.ogawa.base.utils.OSUtils
import com.ogawa.joypal.distributionnetwork.R
import com.ogawa.projectcommon.constants.UrlPathConstant
import com.yanzhenjie.permission.AndPermission
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity() {
    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }
    var viewName = ""
    var downRunnable = object :Runnable {
        override fun run() {
            LogUtils.e("当前长按：${viewName}")
            mHandler.postDelayed(this, 1000)
        }
    }

    var mWifiManager: WifiManager? = null
    override fun initView() {
        super.initView()


        // 获取WifiManager对象
        mWifiManager = this.getApplicationContext()
            .getSystemService(Context.WIFI_SERVICE) as WifiManager

        AndPermission.with(this)
            .runtime()
            .permission(
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.CHANGE_WIFI_STATE
            )
            .onGranted {}
            .onDenied { }
            .start()
        WiFiManager.getInstance(this).registerBroadcastReceivers(this)
        tv_add_device.setOnClickListener {
            ARouter.getInstance().build(UrlPathConstant.ACTIVITY_DISTRIBUTIONNETWORK_START_CONNECT)
                .navigation()
        }

        openWifiBt.setOnClickListener {
            //小米需要请求权限
            WiFiManager.getInstance(this).openWiFi()
        }
        closeWifiBt.setOnClickListener {
            //小米需要请求权限
            WiFiManager.getInstance(this).closeWiFi()
        }
        linkWifiBt.setOnClickListener {
//            //小米需要请求权限
//            val connectOpenNetwork = WiFiManager.getInstance(this).connectOpenNetwork("adsasd")
//            LogUtils.e("connectOpenNetwork: ${connectOpenNetwork}")

            val stratTime = System.currentTimeMillis()
            val disconnect = mWifiManager!!.disconnect()
            if (OSUtils.isMiui()) {
                var endTime = System.currentTimeMillis()
                if (endTime - stratTime > 200) {
                    MiuiUtils.jumpToPermissionsEditorActivity(this)
                    return@setOnClickListener
                }
            }
            LogUtils.e("disconnect:${disconnect}")

        }
        linkWifi2Bt.setOnClickListener {
            //小米需要请求权限
//            WiFiManager.getInstance(this).connectAp()
        }

        longTouchTestBt1.setOnTouchListener { view, motionEvent ->
            if (motionEvent.action == MotionEvent.ACTION_DOWN) {
                viewName = "bt1"
                mHandler.post(downRunnable)
            }else if (motionEvent.action == MotionEvent.ACTION_UP){
                mHandler.removeCallbacks(downRunnable)
            }

            return@setOnTouchListener true
        }
        longTouchTestBt2.setOnTouchListener { view, motionEvent ->
            if (motionEvent.action == MotionEvent.ACTION_DOWN) {
                viewName = "bt2"
                mHandler.post(downRunnable)
            }else if (motionEvent.action == MotionEvent.ACTION_UP){
                mHandler.removeCallbacks(downRunnable)
            }

            return@setOnTouchListener true
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        WiFiManager.getInstance(this).onDestroy(this)
    }
}
